#include "core.h"


#include <QIcon>

#include "../lib/shared/widgetinterface.h"
#include "../include/resourceinterface.h"
#include "../lib/shared/qproject.h"
#include "../lib/shared/qwidgetfactory.h"

core::core():
    m_resource(NULL)
{
    QAction *ac;
    ac=new QAction(NULL);
    ac->setIcon(QIcon(":/images/filesave.png"));
    ac->setToolTip("Save");
    ac->setText("Save");
    m_actions.insert("Save_Action",ac);

    ac=new QAction(NULL);
    ac->setIcon(QIcon(":/images/filenew.png"));
    ac->setToolTip("New File");
    ac->setText("New File");
    m_actions.insert("New_Action",ac);

    ac=new QAction(NULL);
    ac->setIcon(QIcon(":/images/fileopen.png"));
    ac->setToolTip("Open Project");
    ac->setText("Open Project");
    m_actions.insert("Open_Action",ac);

    ac=new QAction(NULL);
    ac->setIcon(QIcon(":/images/run.png"));
    ac->setToolTip("Run");
    ac->setText("Run");
    m_actions.insert("Run_Action",ac);

    ac=new QAction(NULL);
    ac->setIcon(QIcon(":/images/close.png"));
    ac->setToolTip("Close");
    ac->setText("Close");
    m_actions.insert("Close_Action",ac);

    connect(QProject::project(),SIGNAL(getResourceData(QString)),this,SLOT(getResourceData(QString)));
}

core::~core()
{
    qDeleteAll(m_actions);
    m_actions.clear();
    delete m_resource;
}

QAction *core::action(QString name)
{
    return m_actions.value(name,NULL);
}

QResourceInterface* core::resourceInterface()
{
    return m_resource;
}

void core::setResourceInterface(QResourceInterface *interface)
{
    m_resource=interface;
}

QWidget* core::createWidget(const QString &name)
{
    return WidgetFactory::create(name);
}

QByteArray core::getResourceData(const QString &name)
{
    if(m_resource==NULL)
    {
        return QByteArray();
    }

    int index=name.indexOf("/");
    QString t,n;
    t=name.left(index);
    n=name.mid(index+1);
    return m_resource->getResourceData(t,n);

}
